// packageAdmin/pages/index/screen/screen.js
import {getDate} from "../../../../utils/Util";

Component({
    options: {
        addGlobalClass: true,
        multipleSlots: true
    },

    /**
     * 组件的属性列表
     */
    properties: {

    },

    /**
     * 组件的初始数据
     */
    data: {
        _isAll: true, //是否显示所有
        _screen: [{
            key: "createTime", //查询字段
            title: "按时间",
            list: [
                {name: "所有", value: ""},
                {name: "今天", value: 0},
                {name: "昨天", value: 0},
                {name: "3天内", value: 0}
            ],
            current: 0
        }, {
            key: "type", //查询字段
            title: "按属性",
            list: [
                {name: "所有", value: ""},
                {name: "寻丢", value: "lost"},
                {name: "还拾", value: "found"},
            ],
            current: 0
        }, {
            key: "verify", //查询字段
            title: "按审核",
            list: [
                {name: "所有", value: ""},
                {name: "未审核", value: 0},
                {name: "通过", value: 1},
                {name: "未通过", value: 2},
            ],
            current: 0
        }, {
            key: "complete", //查询字段
            title: "按状态",
            list: [
                {name: "所有", value: ""},
                {name: "未完成", value: 0},
                {name: "已完成", value: 1}
            ],
            current: 0
        }],
        _currentScreen: null, //当前操作对象
        _actionSheet: {
            show: false,
            list: []
        },
        _params: {}
    },

    /*
    * 初始化数据
    * */
    ready: function () {
        let currentTimes = new Date(getDate(new Date().getTime()).split(" ")[0]).getTime();
        this.setData({
            ["_screen[0].list"]: [
                {name: "所有"},
                {name: "今天", value: currentTimes},
                {name: "昨天", value: currentTimes - 24 * 3600 * 1000},
                {name: "3天内", value: currentTimes - 36 * 3600 * 1000}
            ]
        })
    },

    /**
     * 组件的方法列表
     */
    methods: {
        /*
        * 筛选点击操作
        * */
        bindScreen: function (e) {
            let index = e.currentTarget.dataset.index;
            let name = e.currentTarget.dataset.name;
            let _screen = this.data._screen;
            if (!name) {
                let currentList = _screen[index].list;
                currentList[_screen[index].current].color = "#57bd6a";
                this.data._currentScreen = index;
                this.setData({
                    _actionSheet: {
                        show: !name,
                        list: _screen[index].list
                    }
                })
            } else if (name && !this.data._isAll) { //重置
                this.setData({
                    "_actionSheet.list": this.data._actionSheet.list.map(function (item) {
                        delete item.color
                        return item
                    }),
                    _screen: _screen.map(function (p) {
                        p.current = 0;
                        return p
                    }),
                    _currentScreen: null,
                    _isAll: true
                })
                this.triggerEvent("screen", {name: "reset", result: {}}, {});
            }
        },

        /*
        * actionSheet接收函数
        * */
        onActionSheetClose() {
            this.setData({ "_actionSheet.show": false });
        },
        onActionSheetSelect(event) {
            const that = this;
            let pInx = that.data._currentScreen;
            let cList = that.data._screen[pInx].list;
            let cInx = that.data._screen[pInx].current
            let currentChildInx = cList.findIndex(function (item) {
                return item.name === event.detail.name
            })
            if (cInx !== currentChildInx) {
                let params = that.data._params;
                let key = that.data._screen[pInx].key;
                currentChildInx !== 0 ? params[key] = event.detail.value : delete params[key];
                that.setData({
                    ["_screen[" + pInx + "].current"]: currentChildInx,
                    ["_actionSheet.list[" + currentChildInx + "].color"]: "#57bd6a",
                    ["_actionSheet.list[" + cInx + "].color"]: "",
                    _params: params
                }, function () {
                    that.setData({
                        _isAll: (that.data._screen.findIndex(function (item) {
                            return item.current > 0
                        })) === -1
                    })
                })
                that.triggerEvent("screen", {name: "add", result: params}, {});
            }
        }
    }
})
